"""
首页使用的数据
"""
from datetime import date, datetime
from sa import models


class IndexData(object):
    """初始化属性"""
    def __init__(self, login_sa, date_obj=None, num=6):
        """
        :param login_sa: 请求的对象，需要传入
        :param date_obj: 默认为 None，此时将使用当前日期
        :param num: 要生成的前几个月份和年份的数量，默认为 6个月
        """
        # 如果提供了日期对象，则使用它；否则，使用当前日期
        self.today = date_obj if date_obj is not None else date.today()
        # 从当前日期中获得当前年份, 当前月份,登录对象
        self.current_year = self.today.year
        self.current_month = self.today.month
        self.user = login_sa
        # 空的年份和月份列表，用于存储计算得出的前几个月份和年份
        self.months = []
        self.years = []
        # 循环计算前几个月份和年份  0-5
        for i in range(num):
            month = self.today.month - i
            year = self.today.year
            if month < 1:
                month += 12
                year -= 1
            self.months.append(month)
            self.years.append(year)
        self.months.reverse()
        self.years.reverse()

    def get_project_count(self):
        """ 根据年份和月份 获得对应project统计数 """
        ERO_count = []
        SRO_count = []
        NRO_count = []
        for i in range(6):
            # start_date__year, start_date__month, 日期类型中的年份和月份
            E_count = models.Project.objects.filter(
                start_date__year=self.years[i], start_date__month=self.months[i], sa__RO='ERO').count()
            S_count = models.Project.objects.filter(
                start_date__year=self.years[i], start_date__month=self.months[i], sa__RO='SRO').count()
            N_count = models.Project.objects.filter(
                start_date__year=self.years[i], start_date__month=self.months[i], sa__RO='NRO').count()
            ERO_count.append(E_count)
            SRO_count.append(S_count)
            NRO_count.append(N_count)
        # 拿到各区SA人员列表
        ERO_sa_list = []
        SRO_sa_list = []
        NRO_sa_list = []
        sa_queryset = models.SA_Info.objects.exclude(Employee_No='0000').filter(role='user').order_by('-id')
        for obj in sa_queryset:
            if obj.RO == 'ERO':
                ERO_sa_list.append(obj.SA_CName)
            if obj.RO == 'SRO':
                SRO_sa_list.append(obj.SA_CName)
            if obj.RO == 'NRO':
                NRO_sa_list.append(obj.SA_CName)
        # 分别拿到各个RO SA最近6个月project个数
        # 初始化一个字典来存储结果 其中键是SA的姓名（字符串），值是一个包含6个整数的列表（代表该SA过去6个月每个月的项目数量）
        NRO_sa_project_counts = {}  # {{"宋志栋": [0 ,0, 0, 0, 0, 0]}, {'王重阳': [0, 0, 0, 0, 0, 0]}}
        for sa_name in NRO_sa_list:
            # 初始化一个列表来存储该SA每个月的项目数量
            monthly_counts = []
            for i in range(6):
                count = models.Project.objects.filter(
                    start_date__year=self.years[i], start_date__month=self.months[i], sa__SA_CName=sa_name
                ).count()
                # 将计数添加到列表中
                monthly_counts.append(count)
            # 将SA名称和对应的项目数量列表存储到字典中
            # {{"宋志栋": [0 ,0, 0, 0, 0, 0]}, {'王重阳': [0, 0, 0, 0, 0, 0]}}
            NRO_sa_project_counts[sa_name] = monthly_counts
        ERO_sa_project_counts = {}
        for sa_name in ERO_sa_list:
            monthly_counts = []
            for i in range(6):
                count = models.Project.objects.filter(
                    start_date__year=self.years[i], start_date__month=self.months[i], sa__SA_CName=sa_name
                ).count()
                monthly_counts.append(count)
            ERO_sa_project_counts[sa_name] = monthly_counts
        # SRO字典，保存每个南区每个SA的6个月数据{{'sa_name1'}:[0,0,0,0,0,0], {'sa_name2'}:[0,0,0,0,0,0]}
        SRO_sa_project_counts = {}
        for sa_name in SRO_sa_list:
            # 列表来存储该SA每个月的项目数量
            monthly_counts = []
            for i in range(6):
                count = models.Project.objects.filter(
                    start_date__year=self.years[i], start_date__month=self.months[i], sa__SA_CName=sa_name
                ).count()
                monthly_counts.append(count)
            # 将SA名称和对应的项目数量列表存储到字典中
            SRO_sa_project_counts[sa_name] = monthly_counts

        return {
            'ERO_projects': ERO_count, 'SRO_projects': SRO_count, 'NRO_projects': NRO_count,
            'ERO_sa_list': ERO_sa_list, 'SRO_sa_list': SRO_sa_list, 'NRO_sa_list': NRO_sa_list,
            'ERO_sa_project_counts': ERO_sa_project_counts,
            'SRO_sa_project_counts': SRO_sa_project_counts,
            'NRO_sa_project_counts': NRO_sa_project_counts,
        }

    def get_task_total_time(self):
        """ 前6月各RO Task 用时统计"""
        ERO_task_time = []
        SRO_task_time = []
        NRO_task_time = []
        for i in range(6):
            ERO_obj_time = 0
            SRO_obj_time = 0
            NRO_obj_time = 0
            E_queryset = models.Task.objects.filter(
                task_date__year=self.years[i], task_date__month=self.months[i], creator__RO='ERO'
            ).all()
            for obj in E_queryset:
                ERO_obj_time += obj.total_time
            ERO_task_time.append(ERO_obj_time / 60)
            S_queryset = models.Task.objects.filter(
                task_date__year=self.years[i], task_date__month=self.months[i], creator__RO='SRO'
            ).all()
            for obj in S_queryset:
                SRO_obj_time += obj.total_time
            SRO_task_time.append(SRO_obj_time / 60)
            N_queryset = models.Task.objects.filter(
                task_date__year=self.years[i], task_date__month=self.months[i], creator__RO='NRO'
            ).all()
            for obj in N_queryset:
                NRO_obj_time += obj.total_time
            NRO_task_time.append(NRO_obj_time / 60)
        return {'ERO_task_time': ERO_task_time, 'SRO_task_time': SRO_task_time, 'NRO_task_time': NRO_task_time}

    def get_task_count(self):
        """ 根据年份所有 Task 统计数  """
        # 当前年份，所有 Task 统计数
        ERO_Task_count = models.Task.objects.filter(
            task_date__year=self.current_year, creator__RO='ERO').count()
        SRO_Task_count = models.Task.objects.filter(
            task_date__year=self.current_year, creator__RO='SRO').count()
        NRO_Task_count = models.Task.objects.filter(
            task_date__year=self.current_year, creator__RO='NRO').count()
        return {
            'ERO_Task_count': ERO_Task_count,
            'SRO_Task_count': SRO_Task_count,
            'NRO_Task_count': NRO_Task_count,
        }

    def get_personal_project_count(self):
        """ 个人前6个月project统计 """
        personal_counts = []
        for i in range(6):
            num = models.Project.objects.filter(
                start_date__year=self.years[i], start_date__month=self.months[i], sa=self.user
            ).count()
            personal_counts.append(num)
        return {'personal_counts': personal_counts}

    def get_personal_task_time(self):
        """ 个人前 6个月 task 用时合计统计 """
        personal_task_total_time = []
        for i in range(6):
            total_time = 0
            task_list = models.Task.objects.filter(
                task_date__year=self.years[i], task_date__month=self.months[i], creator=self.user
            ).all()
            for obj in task_list:
                total_time += obj.total_time
            personal_task_total_time.append(total_time / 60)
        return {'personal_task_total_time': personal_task_total_time}


if __name__ == '__main__':
    pass